#include "s_desk_monitor.h"
#include "h_stm32_hal_defines.h"
#include <stdio.h>
#include <string.h>
// #include <stdlib.h>
#include "s_common_message.h"
#include "s_lcd.h"
#include "asus.h"

#define LCD_CMD								"LCD"

typedef struct
{
	unsigned short x;
	unsigned short y;
	unsigned short fc;
	unsigned short bc;
	unsigned char *data;
	unsigned short len;
	unsigned short size;	
	unsigned short split_char_num;
}lcd_data_t;

lcd_data_t s_desk_monitor_check(unsigned char *data, unsigned short len)
{
	unsigned short i;
	lcd_data_t data_t;
	// unsigned short temp;
	unsigned char *t_char = NULL;
	// unsigned short t_len = 0;
	unsigned short t_char_start_index;

	data_t.split_char_num = 0U;

	if(data[0] != ':') {
		return data_t;
	}

	for(i = 1; i < len; i++) {
		if(data[i] == ',') {
			switch(data_t.split_char_num) {
				case 0:
					memcpy(&data_t.x, &data[i - 2], 2);
					break;

				case 1:
					memcpy(&data_t.y, &data[i - 2], 2);
					break;

				case 2:
					memcpy(&data_t.fc, &data[i - 2], 2);
					break;

				case 3:
					memcpy(&data_t.bc, &data[i - 2], 2);
					t_char = &data[i + 1];
					t_char_start_index = i;
					// printf("start index is %d\r\n", t_char_start_index);
					break;

				case 4:
					data_t.len = i - t_char_start_index - 1;
					// printf("the len is %d-%d\r\n", data_t.len, i);
					// memcpy(&data_t.y, &data[i - 2], 2);
					// t_char = &data[i - 2];
					// printf("%s\n", t_char);
					break;

				// case 5:
				// 	memcpy(&data_t.len, &data[i - 2], 2);
				// 	break;

				// case 5:
				// 	memcpy(&data_t.size, &data[i - 2], 2);
				// 	break;

				default:
					break;
			}
			data_t.split_char_num ++;
		}
	}

	memcpy(&data_t.size, &data[len - 2], 2);

	if(0 == data_t.len) {
		data_t.data = NULL;
	} else {

		data_t.data = (unsigned char *)H_STM32_MALLOC(sizeof(unsigned char) * data_t.len);
		if(data_t.data == NULL) {
			data_t.split_char_num = 0U;
		}
		memcpy(data_t.data, t_char, data_t.len);
	}

	// printf("split_char_num is %d\r\n", data_t.split_char_num);

	return data_t;
}

void s_desk_monitor_lcd_callback(const unsigned char *data, unsigned short len)
{

	lcd_data_t lcd_data;

	lcd_data = s_desk_monitor_check(data, len);

	if(lcd_data.split_char_num == 0) {
		return;
	}

	// printf("the x is %d, y is %d, fc is %d, bc is %d, len is %d, size is %d\r\n", lcd_data.x, \
	// 	lcd_data.y, lcd_data.fc, lcd_data.bc, lcd_data.len, lcd_data.size);

	// s_lcd_show_string(lcd_data.x, lcd_data.y, lcd_data.fc, lcd_data.bc, "lcd_data.data", lcd_data.len, lcd_data.size);
	s_lcd_show_string(lcd_data.x, lcd_data.y, lcd_data.fc, lcd_data.bc, lcd_data.data, lcd_data.len, lcd_data.size);

	// printf("--%s--\n", lcd_data.data);

	if(lcd_data.data != NULL) {
		H_STM32_FREE(lcd_data.data);
		lcd_data.data = NULL;
	}
}

signed char s_desk_monitor_init(void)
{
	s_common_message_init();

	// s_lcd_show_pic(0, 0, 64, 128, gImage_asus);

	// s_lcd_fill(0,0,128,32,WHITE);
	// // s_lcd_show_string(20,30,BLUE,YELLOW,"BL Test", 10,16);
	// s_lcd_fill(0,32,128,32,RED);
	// // s_lcd_show_string(20,30,BLUE,YELLOW,"RED ", 10,16);
	// s_lcd_fill(0,64,128,32,GREEN);
	// // s_lcd_show_string(20,30,BLUE,YELLOW,"GREEN ", 10,16);
	// s_lcd_fill(0,96,128,32,BLUE);
	// // s_lcd_show_string(20,30,RED,YELLOW,"BLUE ", 10,16);

	s_lcd_show_pic(0, 0, 128, 128, gImage_asus);

	printf("Add lcd rslt %d\r\n", s_common_message_add(LCD_CMD, STRLEN(LCD_CMD), &s_desk_monitor_lcd_callback));

	S_Lock_Buzzer_Success();
	return 0;
}
